<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in" ref="dialogBox">
      <div class="cornerComment-in-title">
        <!--        type 0 学生 2 职工-->
        <span>编辑算法</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <div class="fiel_body">
          <div class="fiel_left">
            <el-form ref="infoForm" :model="infoForm" :rules="rules" label-width="140px">
              <el-form-item label="区角评价维度:" prop="title">
                <el-input v-model="infoForm.title" placeholder="填写区角评价维度"></el-input>
              </el-form-item>
              <el-form-item label="选择关联领域:" prop="domain_ids">
                <div>
                  <el-select placeholder="" v-model="first_field" size="small"
                             class="w-140 mr-10">
                    <el-option :label="item.name" :value="item.id" v-for="(item, index) in list01"
                               :key="index"></el-option>
                  </el-select>
                  <el-select placeholder="" v-model="second_field" size="small"
                             class="w-140 mr-10" v-if="list01[first_field]">
                    <el-option :label="item.name" :value="item.id" v-for="(item, index) in list01[first_field].list"
                               :key="index"></el-option>
                  </el-select>
                  <el-button type="primary" @click="checkTabLine()" class="mb-10" size="small">添加领域</el-button>
                </div>
                <div>
                  <el-tag
                    :key="tag"
                    v-for="(tag,index) in dynamicTags"
                    closable
                    :disable-transitions="false"
                    @close="handleClose(index)"
                    class="mr-10"
                  >{{ tag }}
                  </el-tag>
                </div>
              </el-form-item>
              <el-form-item label="描述:" prop="description">
                <el-input v-model="infoForm.description" placeholder="" type="textarea"
                          placeholder="填写区角名称和右侧矩阵图三级分类"></el-input>
              </el-form-item>
            </el-form>
          </div>
          <div class="fiel_right">
            <img src="@/assets/imageAdmin/field.jpg" alt="">
          </div>
        </div>
      </div>
      <div class="cornerComment-in-footer">
        <el-button type="primary" @click="onSubmit">确认并保存</el-button>
        <el-button @click="closePOpup" size="small">返回</el-button>
      </div>
    </div>
    <print :visible.sync="printvisible" :studentInfo="studentInfo" type="1"></print>
  </div>
</template>

<script>
import {pagination} from '@/utils/util.js'
import {questionnaireUser, questionnaireUnread, questionnaireDelete} from '@/api/admin/question'
import print from '@/components/dialog/print.vue'

export default {
  props: ['visible', 'detailId'], // listType 1 签收 2 未签收
  components: {print},
  data() {
    return {
      healthId: 0,
      dialogVisible: this.visible,
      arryList: [], // 数据列表
      printvisible: false,
      studentInfo: {},
      // 页数
      page: {
        pageindex: pagination.pageIndex,
        pagesize: pagination.pageSize,
        total: pagination.total
      },
      tableHeight: 0,
      detailVisible: false,
      commitmentStatus: false,
      infoForm: {
        title: '',
        domain_ids: '',
        description: ''
      },
      // 表单验证
      rules:
        {
          title: [
            {required: true, message: '请输入区角属性名称', trigger: 'blur'}
          ],
          domain_ids: [
            {required: true, message: '所属领域', trigger: 'blur'}
          ],
          description: [
            {required: true, message: '简介', trigger: 'blur'}
          ]
        },
      dynamicTags: [],
      dynamicTagsId: [],
      // 重新构建
      first_field: '',
      second_field: '',
      list01: {}
    }
  },
  watch: {
    visible() {
      if (this.visible) {
        let n = this;
        this.$nextTick(() => {
          let height = n.$refs.dialogBox.offsetHeight;  //top 50 1 search 50 page 50 10 footer 50 1
          this.tableHeight = height - (51 + 20 + 50 + 51)
        })
        this.getLsitFn()
      } else {
        this.arryList = [];
        this.page.pageindex = 1
        this.page.total = 0
        this.dynamicTags = []
      }
      this.dialogVisible = this.visible
    }
  },
  created() {
  },
  methods: {
    getLsitFn () {
      this.$fetch(this.api.corner.domain.index, {}).then((response) => {
        console.log('数据：', response)
        if (response.code === 0) {
          let list01 = {}
          for (let i in response.list) {
            if (!response.list[i].parent_id) {
              list01[response.list[i].id] = response.list[i]
              list01[response.list[i].id].list = {}
            } else {
              list01[response.list[i].parent_id].list[response.list[i].id] = response.list[i]
            }
          }
          console.log('查看数据：', list01)
          this.list01 = list01
          if (this.detailId) {
            this.InitFn()
          }
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    },
    checkTabLine () {
      if (!this.first_field || !this.second_field) {
        this.$message.error('请选择完整的领域')
        return
      }
      if (this.dynamicTagsId.indexOf(this.second_field) !== -1) {
        this.$message.error('领域已存在')
        return
      }
      this.dynamicTags.push(this.list01[this.first_field].list[this.second_field].name)
      this.dynamicTagsId.push(this.list01[this.first_field].list[this.second_field].id)
      this.infoForm.domain_ids = this.dynamicTagsId.join(',')
    },
    // 初始化
    async InitFn() {
      this.$fetch(this.api.corner.attr.edit, {id: this.detailId}).then((response) => {
        if (!response.code) {
          for (let i in response.vo.domain_ids) {
            this.dynamicTagsId.push(response.vo.domain_ids[i].id)
            this.dynamicTags.push(response.vo.domain_ids[i].name)
          }
          this.infoForm.title = response.vo.title
          this.infoForm.domain_ids = this.dynamicTagsId.join(',')
          this.infoForm.description = response.vo.description
        } else {
          console.log('请求失败')
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    handleSizeChange(val) {
      this.page.pageindex = val
      this.InitFn()
    },
    checkStudent(index) {
      this.studentInfo = this.arryList[index]
      this.printvisible = true
    },
    onSubmit () {
      console.log('数据提交', this.infoForm)
      if (!this.infoForm.title) {
        this.$message.error('请填写区角属性名称')
        return false
      }
      if (!this.infoForm.domain_ids) {
        this.$message.error('请选择区角属性领域')
        return false
      }
      if (!this.infoForm.description) {
        this.$message.error('请填写属性描述')
        return false
      }
      let http_ = this.detailId ? this.api.corner.attr.edit : this.api.corner.attr.add
      if (this.detailId) {
        this.infoForm.id = this.detailId
      }
      this.$post(http_, this.infoForm).then((response) => {
        if (response.code === 0) {
          this.$message.success(response.msg)
          if (!this.$route.query.id) {
            this.infoForm.title = ''
            this.infoForm.domain_ids = ''
            this.infoForm.description = ''
            this.dynamicTags = []
            this.dynamicTags = []
            this.first_field = ''
            this.second_field = ''
          }
        this.closePOpup()
        } else {
          this.$message.error(response.msg)
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    handleDetel(id) {
      this.$confirm('确认删除该生签收记录,删除后家长可重新签收 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        questionnaireDelete({id: id}).then(res => {
          if (!res.code) {
            this.$message.success(res.msg)
            this.InitFn()
            this.$emit('InitFn')
          } else {
            this.$message.error(res.msg)
          }
        })
      }).catch(() => {
      })
    },
    handleClose (index) {
      this.dynamicTags.splice(index, 1)
      this.dynamicTagsId.splice(index, 1)
      this.infoForm.domain_ids = this.dynamicTagsId.join(',')
    },
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 80%;
  height: 70%;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 10px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

.toolbarLine {
  text-align: center;
}

.hearder-search {
  display: flex;
  align-items: center;
}

.breadcrumb-container {
  margin-bottom: 10px;
}

.parentsList p {
  background: #f8f8f8;
  margin-bottom: 5px;
}

.parentsList p:last-child {
  margin-bottom: 0;
}
</style>


